package com.devsoft.foodfrenzy;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.TiledSprite;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.util.GLState;
import org.andengine.util.adt.align.HorizontalAlign;
import org.andengine.util.adt.color.Color;
import com.devsoft.foodfrenzy.SceneManager.SceneType;

public class PlayerInfoScene extends BaseScene{
	
	private Text title_Text;
	private Text first_Text;
	private Text second_Text;
	private Text third_Text;
	private Text fourth_Text;
	private Text fifth_Text;
	private Text sixth_Text;
	private TiledSprite current_skin;
	private Sprite back_button;
	private Sprite reset_button;
	private Sprite next_button;
	private Sprite warning_popup;
	private Sprite yes_button;
	private Sprite no_button;
	private boolean warning_flag = false;
	private int screen_index = 1;
	
	private void createText()
    {
     	
        title_Text = new Text(255, 300, resourcesManager.font, "ClassicArcadeMultiPlayerMode:", new TextOptions(HorizontalAlign.LEFT), vbom);
        title_Text.setAnchorCenter(0, 0);
        title_Text.setColor(Color.BLACK);
        title_Text.setText("Classic Mode:");
        
        first_Text = new Text(255, 240, resourcesManager.font, "HighScoreGamesWon:0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
        first_Text.setAnchorCenter(0, 0);
        first_Text.setColor(Color.BLACK);
        first_Text.setText("High Score: " + UserData.getInstance().getHighScore(0)); 
        
        second_Text = new Text(255, 180, resourcesManager.font, "GamesPlayed:0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
        second_Text.setAnchorCenter(0, 0);
        second_Text.setColor(Color.BLACK);
        second_Text.setText("Games Played: " + UserData.getInstance().getUnlocksVal(1)); 
        
        third_Text = new Text(255, 330, resourcesManager.font, "FoodsFlippedMAXComboChain:", new TextOptions(HorizontalAlign.LEFT), vbom);
        third_Text.setAnchorCenter(0, 0);
        third_Text.setColor(Color.BLACK);
        third_Text.setText("");
        
        fourth_Text = new Text(255, 270, resourcesManager.font, "0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
        fourth_Text.setAnchorCenter(0, 0);
        fourth_Text.setColor(Color.BLACK);
        fourth_Text.setText("");
        
        fifth_Text = new Text(255, 210, resourcesManager.font, "FoodsCookedTOTALGamesPlayed:", new TextOptions(HorizontalAlign.LEFT), vbom);
        fifth_Text.setAnchorCenter(0, 0);
        fifth_Text.setColor(Color.BLACK);
        fifth_Text.setText("");
        
        sixth_Text = new Text(255, 150, resourcesManager.font, "0123456789", new TextOptions(HorizontalAlign.LEFT), vbom);
        sixth_Text.setAnchorCenter(0, 0);
        sixth_Text.setColor(Color.BLACK);
        sixth_Text.setText("");
        
        attachChild(title_Text);
        attachChild(first_Text);
        attachChild(second_Text);
        attachChild(third_Text);
        attachChild(fourth_Text);
        attachChild(fifth_Text);
        attachChild(sixth_Text);
        
    }
	
	private void createImages()
	{
		current_skin = new TiledSprite(125, 250, resourcesManager.player_skin_region, vbom);
	    current_skin.setCurrentTileIndex(UserData.getInstance().getSkinPref());
	    attachChild(current_skin);
	    
	    next_button = new Sprite(675, 48, resourcesManager.pi_next_region, vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	if(!warning_flag)
                	{
                		if(screen_index < 5)
                		{
                			screen_index++;
                		}
                		else
            			{
                			screen_index = 1;
            			}
                		updateHUD();
                	}
                }
                return true;
            };
        };
        registerTouchArea(next_button);
        attachChild(next_button);
        
	    reset_button = new Sprite(400, 48, resourcesManager.pi_reset_region, vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	if(!warning_flag)
                	{
                		warning_popup.setVisible(true);
                		yes_button.setVisible(true);
                		no_button.setVisible(true);
                		warning_flag = true;
                	}
                }
                return true;
            };
        };
        registerTouchArea(reset_button);
        attachChild(reset_button);
        
	    back_button = new Sprite(125, 48, resourcesManager.pi_back_region, vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	if(!warning_flag)
                	{
                		if(screen_index > 1)
                		{
                			screen_index--;
                		}
                		else
            			{
                			screen_index = 5;
            			}
                		updateHUD();
                	}
                }
                return true;
            };
        };
        registerTouchArea(back_button);
        attachChild(back_button);
        
        warning_popup = new Sprite(400, 240, resourcesManager.pi_warning_region, vbom);
	    warning_popup.setVisible(false);
	    attachChild(warning_popup);
	    
	    yes_button = new Sprite(310, 150, resourcesManager.pi_yes_region, vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	UserData.getInstance().resetUnlocksCode();
                	warning_popup.setVisible(false);
                	yes_button.setVisible(false);
                	no_button.setVisible(false);
                	warning_flag = false;
                	current_skin.setCurrentTileIndex(UserData.getInstance().getSkinPref());
                	updateHUD();
                }
                return true;
            };
        };
        registerTouchArea(yes_button);
        yes_button.setVisible(false);
        attachChild(yes_button);
        
	    no_button = new Sprite(490, 150, resourcesManager.pi_no_region, vbom){
            public boolean onAreaTouched(TouchEvent touchEvent, float X, float Y)
            {
                if (touchEvent.isActionUp())
                {
                	warning_flag = false;
                	warning_popup.setVisible(false);
                	yes_button.setVisible(false);
                	no_button.setVisible(false);
                }
                return true;
            };
        };
        registerTouchArea(no_button);
        no_button.setVisible(false);
        attachChild(no_button);
	}
	
	public void updateHUD()
    {
		switch(screen_index){
		case 1:
			title_Text.setText("Classic Mode:");
			first_Text.setText("High Score: " + UserData.getInstance().getHighScore(0));
			second_Text.setText("Games Played: " + UserData.getInstance().getUnlocksVal(1));
			third_Text.setText("");
			fourth_Text.setText("");
			fifth_Text.setText("");
			sixth_Text.setText("");
			break;
		case 2:
			title_Text.setText("Arcade Mode:");
			first_Text.setText("High Score: " + UserData.getInstance().getHighScore(1));
			second_Text.setText("Games Played: " + UserData.getInstance().getUnlocksVal(2));
			third_Text.setText("");
			fourth_Text.setText("");
			fifth_Text.setText("");
			sixth_Text.setText("");
			break;
		case 3:
			title_Text.setText("MultiPlayer Mode:");
			first_Text.setText("Games Won: " + UserData.getInstance().getUnlocksVal(7));
			second_Text.setText("Games Played: " + UserData.getInstance().getUnlocksVal(3));
			third_Text.setText("");
			fourth_Text.setText("");
			fifth_Text.setText("");
			sixth_Text.setText("");
			break;
		case 4:
			title_Text.setText("");
			first_Text.setText("");
			second_Text.setText("");
			third_Text.setText("Foods Flipped:");
	        fourth_Text.setText("" + UserData.getInstance().getUnlocksVal(4));
	        fifth_Text.setText("Foods Cooked:");
			sixth_Text.setText("" + UserData.getInstance().getUnlocksVal(5));
			break;
		case 5:
			title_Text.setText("");
			first_Text.setText("");
			second_Text.setText("");
			third_Text.setText("MAX Combo Chain:");
	        fourth_Text.setText("" + UserData.getInstance().getUnlocksVal(6));
	        fifth_Text.setText("TOTAL Games Played:");
			sixth_Text.setText("" + (UserData.getInstance().getUnlocksVal(1) + UserData.getInstance().getUnlocksVal(2)));
		break;
		}
    }
	
	@Override
	public void createScene() 
	{
		createBackground();
	    createText();
	    createImages();
	}
	
	private void createBackground()
	{
	    attachChild(new Sprite(400, 240, resourcesManager.playerinfo_background_region, vbom)
	    {
	        @Override
	        protected void preDraw(GLState pGLState, Camera pCamera) //makes picture quality smoother
	        {
	            super.preDraw(pGLState, pCamera);
	            pGLState.enableDither();
	            
	        }
	        
	    });
	    
	}

	@Override
	public void onBackKeyPressed() 	//exit app when backed
	{
		SceneManager.getInstance().loadMenuScene(engine, this.getSceneType());
	}
	

	@Override
	public SceneType getSceneType()
	{
	    return SceneType.SCENE_INFO;
	}
	
	
	@Override
	public void disposeScene() 
	{
		this.detachSelf();
        this.dispose();
	}

}